package com.inventory.Controller;


import com.alibaba.fastjson2.util.DateUtils;
import com.common.entity.Result;
import com.common.util.TimeUtils;
import com.github.pagehelper.PageInfo;
import com.inventory.Pojo.GoodsNum;
import com.inventory.Pojo.Inventory;
import com.inventory.Pojo.Outgoods;
import com.inventory.Service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.TemporalAdjusters;
import java.util.*;


@RestController
@RequestMapping("/inventory")
public class InventoryController {
    @Autowired
    private InventoryService inventoryService;
    //添加
    @PostMapping("/AddInventory")
    public Result AddInventory(@RequestBody Inventory inventory){
        Inventory i=inventoryService.getBygoodsId(inventory.getGoodsId());
        if (i==null){
            inventoryService.AddInventory(inventory);
            return  Result.success();
        }else {
            return Result.error("此商品已添加库存信息");
        }

    }
    //分页查询
    @GetMapping("/SearchInventory")
    public Result get(@RequestParam Integer pageNum,
                      @RequestParam Integer pageSize,
                      @RequestParam String goodsId,
                      @RequestParam String storehouseId){
        PageInfo<Inventory> inventoryPageInfo=inventoryService.SearchInventory(pageNum,pageSize,goodsId,storehouseId);
        return Result.success(inventoryPageInfo);
    }
    //添加库存
    @PostMapping("/AddInNum")
    public Result addIn(@RequestBody GoodsNum num){
        Inventory inventory=inventoryService.getByid(num.getInventoryId());
        Integer oldNum=inventory.getInNum();

        Integer inNum=num.getInNum();
        Integer newNum= oldNum+inNum;

//        Date date=new Date();
//        String intime =DateUtils.toString(date);
//
//
//
//        inventoryService.AddInNum(newNum,num.getInventoryId(),intime);


        Integer lastmonthNum = 0;

        LocalDate today = LocalDate.now();
        inventory.setInTime(today.toString());
        //判断日期是否为本月最后一天
        LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());

        // true表示是最后一天，false表示不是最后一天
       // if(Period.between(today, lastDayOfMonth).getDays() == 0){
        if(Period.between(today, lastDayOfMonth).getDays() == 0){
            //把上个月的出库数量村给LastmonthNum
            lastmonthNum=inventory.getOutNum();
        }

       inventoryService.AddInNum(newNum,num.getInventoryId(), String.valueOf(today),lastmonthNum);
        return Result.success();
    }
    //出库
    @PostMapping("/out")
    public Result out(@RequestBody GoodsNum goodsNum) throws ParseException {
        Inventory inventory=inventoryService.getByid(goodsNum.getInventoryId());

        //更新出库时间
        Date date=new Date();
        String outtime =DateUtils.toString(date);

        Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(outtime);

        if(inventory.getOutTime() != null){
            Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(inventory.getOutTime());

            boolean result = TimeUtils.isSameMonth(date1, date2);
            Inventory inventory1 =new Inventory();
            if(result ){
                //计算出货数量
                Integer outOldNum=inventory.getOutNum();
                Integer num= goodsNum.getOutNum();
                Integer outSum=outOldNum+num;
                //计算存货数量
                Integer oldNum=inventory.getInNum();
                Integer newNum=oldNum-num;

                if(newNum>=0){
                    inventoryService.OutNum(newNum,outSum,outtime, goodsNum.getInventoryId());
                    return Result.success();
                }else {
                    return Result.error("商品库存数量不足");
                }
            }


            else {
                inventory1 =inventory;
                System.out.println("````````````"+inventory1+"``````````````");
                inventory1.setOutNum(goodsNum.getOutNum());
                inventory1.setInNum(inventory.getInNum()-goodsNum.getOutNum());
                inventory1.setInventoryId(0);
                inventory1.setOutTime(outtime);
            }
            inventoryService.addNew(inventory1);
        }else {
            //计算出货数量
            Integer outOldNum=inventory.getOutNum();
            Integer num= goodsNum.getOutNum();
            Integer outSum=outOldNum+num;
            //计算存货数量
            Integer oldNum=inventory.getInNum();
            Integer newNum=oldNum-num;

            if(newNum>=0){
                inventoryService.OutNum(newNum,outSum,outtime, goodsNum.getInventoryId());
                return Result.success();
            }else {
                return Result.error("商品库存数量不足");
            }
        }


            return Result.success();
    }

    //删库存信息
    @DeleteMapping("/DelInventory")
    public Result Del(@RequestParam Integer Id){
        Inventory inventory=inventoryService.getByid(Id);
        if (inventory.getInNum()==0){
            inventoryService.Del(Id);
            return Result.success();
        }else {
            return  Result.error("还有存量,不可删除！");
        }
    }

    //修改
    @PutMapping("/UpdateInventory")
    public Result Update(@RequestBody Inventory inventory){
        inventoryService.updateInventory(inventory);
        return  Result.success();
    }



    //出库记录
    @PostMapping("/outgoods")
    public Result outgoods(@RequestBody Outgoods outgoods){
        Inventory inventory=inventoryService.getByid(outgoods.getInventoryId());
        float inprice=inventory.getInPrice();
        float outprice=inventory.getOutPrice();
        int profit= (int) (outprice-inprice);
        int outgoodsnum=outgoods.getOutgoodsNum();
        outgoods.setTotalProfit(profit*outgoodsnum);
        inventoryService.outgoods(outgoods);
        return Result.success();
    }
    //出库记录查询
    @GetMapping("/Searchoutgoods")
    public Result searchoutgoods(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam String goodsId){
        PageInfo<Outgoods> outgoodsPageInfo=inventoryService.searchoutgoods(pageNum,pageSize,goodsId);
        return Result.success(outgoodsPageInfo);

    }

    /**
     * 可视化查询
     * @return
     */
    @GetMapping("/mid")
    public Result mid(){
        List<Inventory> list =  inventoryService.get4();
        float total =0;
        for (int i = 0; i < list.size(); i++) {
        float a =  (list.get(i).getOutPrice()*list.get(i).getOutNum());
        total += a;
        }
        return Result.success(total);

    }

    @GetMapping("/mid1")
    public Result mid1(){
        List<Inventory> list =  inventoryService.get4();
        float profit =0;
        for (int i = 0; i < list.size(); i++) {
            float a =  ((list.get(i).getOutPrice()-list.get(i).getInPrice())*list.get(i).getOutNum());
            profit += a;
        }
        return Result.success(profit);

    }

    @GetMapping("/Search")
    public Result get(){
        List<Inventory> list =  inventoryService.get();

        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Inventory value : list) {
            Map<String,Object> map = new HashMap<>();
            map.put("name",value.getGoodsName());
            map.put("value",value.getOutNum());
            mapList.add(map);
        }
        return Result.success(mapList);

    }

    @GetMapping("/Search2")
    public Result get2 () {
        List<Inventory> list2 = inventoryService.get2();

        List<Map<String, Object>> mapList2 = new ArrayList<>();
        for (Inventory value : list2) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", value.getGoodsName());
            map.put("value", value.getInNum());
            mapList2.add(map);
        }
        return Result.success(mapList2);

    }

    @GetMapping("/Search1")
    public Result get1() throws ParseException {
        List<Inventory> list1 = inventoryService.get1();

        List<Map<String, Object>> mapList1 = new ArrayList<>();

        /**
         * 遍历每一条数据，并对每一条数据里的入库时间和出库时间做比较，判断是否为一个月
         */
        for (int i = 0; i < list1.size(); i++) {
            Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(list1.get(i).getInTime());
            Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(list1.get(i).getOutTime());
            boolean result = TimeUtils.isSameMonth(date1, date2);
           //System.out.println(result);//ture

            if (result) {

                for (Inventory value : list1) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("name", value.getGoodsName());
                    map.put("value", value.getOutNum());

                    map.put("key",value.getLastmonthNum());
                    mapList1.add(map);
                }

            }

        }
        System.out.println(mapList1);
        return Result.success(mapList1);
    }

    @GetMapping("/Search3")
    public Result get3(){

        List<Inventory> list =  inventoryService.get3();

        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Inventory value : list) {
            Map<String,Object> map = new HashMap<>();
            map.put("name",value.getGoodsName());
            map.put("value",value.getOutNum()* value.getOutPrice());

            mapList.add(map);
        }
        return Result.success(mapList);

    }

    @GetMapping("/Search4")
    public Result get4(){

        List<Inventory> list =  inventoryService.get4();

        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Inventory value : list) {
            Map<String,Object> map = new HashMap<>();
            map.put("name",value.getGoodsName());
            map.put("value",value.getOutNum()* (value.getOutPrice()-value.getInPrice()));

            mapList.add(map);
        }
        return Result.success(mapList);

    }

    @GetMapping("/Search5")
    public Result get5 () {
        List<Inventory> list2 = inventoryService.get4();

        List<Map<String, Object>> mapList2 = new ArrayList<>();
        for (Inventory value : list2) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", value.getGoodsName());
            map.put("value", value.getOutPrice()- value.getInPrice());
            mapList2.add(map);
        }
        return Result.success(mapList2);

    }

    @GetMapping("/yuce")
    public Result yuce() throws ParseException {

        List<Inventory> list1 =  inventoryService.xiaoliang();

        List<Map<String, Object>> mapList1 = new ArrayList<>();
        for (Inventory value : list1) {
            Map<String,Object> map = new HashMap<>();
            Date date = new Date();

            map.put("name",date.getMonth()+2+"月");
            map.put("value",value.getAvarageOutNum());

            mapList1.add(map);
        }


        List<Inventory> list =  inventoryService.yuce();

        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Inventory value : list) {
            Map<String,Object> map = new HashMap<>();
            Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value.getOutTime());
            System.out.println(date);

            map.put("name",date.getMonth()+1+"月");
            map.put("value",value.getOutNum());

            mapList.add(map);
        }
        mapList.addAll(mapList1);
        System.out.println(mapList);
        return Result.success(mapList);

    }
    @GetMapping("/yuce1")
    public Result yuce1() throws ParseException {
        List<Inventory> list1 =  inventoryService.xiaoliang1();

        List<Map<String, Object>> mapList1 = new ArrayList<>();
        for (Inventory value : list1) {
            Map<String,Object> map = new HashMap<>();
            Date date = new Date();
            System.out.println(date);

            map.put("name",date.getMonth()+2+"月");
            System.out.println(date.getMonth()+2+"月");
            map.put("value",value.getAvarageOutNum());

            mapList1.add(map);
        }


        List<Inventory> list =  inventoryService.yuce1();

        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Inventory value : list) {
            Map<String,Object> map = new HashMap<>();
            Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value.getOutTime());
            System.out.println(date);

            map.put("name",date.getMonth()+1+"月");
            map.put("value",value.getOutNum());

            mapList.add(map);
        }
        mapList.addAll(mapList1);
        return Result.success(mapList);

    }

    @GetMapping("/yuce2")
    public Result yuce2() throws ParseException {
        List<Inventory> list1 =  inventoryService.xiaoliang2();

        List<Map<String, Object>> mapList1 = new ArrayList<>();
        for (Inventory value : list1) {
            Map<String,Object> map = new HashMap<>();
            Date date = new Date();
            System.out.println(date);

            map.put("name",date.getMonth()+2+"月");
            System.out.println(date.getMonth()+2+"月");
            map.put("value",value.getAvarageOutNum());

            mapList1.add(map);
        }

        List<Inventory> list =  inventoryService.yuce2();

        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Inventory value : list) {
            Map<String,Object> map = new HashMap<>();
            Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value.getOutTime());
            System.out.println(date);

            map.put("name",date.getMonth()+1+"月");
            map.put("value",value.getOutNum());

            mapList.add(map);
        }
        mapList.addAll(mapList1);
        return Result.success(mapList);

    }


    @GetMapping("/yuce3")
    public Result yuce3() throws ParseException {
        List<Inventory> list1 =  inventoryService.xiaoliang3();

        List<Map<String, Object>> mapList1 = new ArrayList<>();
        for (Inventory value : list1) {
            Map<String,Object> map = new HashMap<>();
            Date date = new Date();
            System.out.println(date);

            map.put("name",date.getMonth()+2+"月");
            System.out.println(date.getMonth()+2+"月");
            map.put("value",value.getAvarageOutNum());

            mapList1.add(map);
        }

        List<Inventory> list =  inventoryService.yuce3();

        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Inventory value : list) {
            Map<String,Object> map = new HashMap<>();
            Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value.getOutTime());
            System.out.println(date);

            map.put("name",date.getMonth()+1+"月");
            map.put("value",value.getOutNum());

            mapList.add(map);
        }
        mapList.addAll(mapList1);
        return Result.success(mapList);

    }

    @GetMapping("/yuce4")
    public Result yuce4() throws ParseException {
        List<Inventory> list1 =  inventoryService.xiaoliang4();

        List<Map<String, Object>> mapList1 = new ArrayList<>();
        for (Inventory value : list1) {
            Map<String,Object> map = new HashMap<>();
            Date date = new Date();
            System.out.println(date);

            map.put("name",date.getMonth()+2+"月");
            System.out.println(date.getMonth()+2+"月");
            map.put("value",value.getAvarageOutNum());

            mapList1.add(map);
        }

        List<Inventory> list =  inventoryService.yuce4();

        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Inventory value : list) {
            Map<String,Object> map = new HashMap<>();
            Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value.getOutTime());
            System.out.println(date);

            map.put("name",date.getMonth()+1+"月");
            map.put("value",value.getOutNum());

            mapList.add(map);
        }
        mapList.addAll(mapList1);
        return Result.success(mapList);

    }

    @GetMapping("/yuce5")
    public Result yuce5() throws ParseException {
        List<Inventory> list1 =  inventoryService.xiaoliang5();

        List<Map<String, Object>> mapList1 = new ArrayList<>();
        for (Inventory value : list1) {
            Map<String,Object> map = new HashMap<>();
            Date date = new Date();
            System.out.println(date);

            map.put("name",date.getMonth()+2+"月");
            System.out.println(date.getMonth()+2+"月");
            map.put("value",value.getAvarageOutNum());

            mapList1.add(map);
        }

        List<Inventory> list =  inventoryService.yuce5();

        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Inventory value : list) {
            Map<String,Object> map = new HashMap<>();
            Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value.getOutTime());
            System.out.println(date);

            map.put("name",date.getMonth()+1+"月");
            map.put("value",value.getOutNum());

            mapList.add(map);
        }
        mapList.addAll(mapList1);
        return Result.success(mapList);

    }





}




